<template>
  <div class="element-inspector">
    <div class="inspector-controls">
      <div class="control-group">
        <label>选择模式</label>
        <div class="radio-group">
          <label class="radio-option">
            <input
                type="radio"
                v-model="inspectMode"
                value="hover"
            />
            <span class="radio-custom"></span>
            悬停检查
          </label>
          <label class="radio-option">
            <input
                type="radio"
                v-model="inspectMode"
                value="click"
            />
            <span class="radio-custom"></span>
            点击检查
          </label>
        </div>
      </div>

      <div class="control-group">
        <button
            class="btn secondary"
            @click="startInspection"
            :disabled="isInspecting"
        >
          <i class="icon-start"></i>
          开始检查
        </button>
        <button
            class="btn danger"
            @click="stopInspection"
            :disabled="!isInspecting"
        >
          <i class="icon-stop"></i>
          停止检查
        </button>
      </div>
    </div>

    <div class="inspector-content">
      <div class="element-info" v-if="selectedElement">
        <h3>元素信息</h3>
        <div class="info-row">
          <span class="label">标签名:</span>
          <span class="value">{{ selectedElement.tagName.toLowerCase() }}</span>
        </div>
        <div class="info-row">
          <span class="label">ID:</span>
          <span class="value">{{ selectedElement.id || '无' }}</span>
        </div>
        <div class="info-row">
          <span class="label">类名:</span>
          <span class="value">{{ selectedElement.className || '无' }}</span>
        </div>
        <div class="info-row">
          <span class="label">尺寸:</span>
          <span class="value">{{ elementSize }}</span>
        </div>
        <div class="info-row">
          <span class="label">位置:</span>
          <span class="value">{{ elementPosition }}</span>
        </div>
      </div>

      <div class="element-styles" v-if="selectedElement">
        <h3>计算样式</h3>
        <div class="styles-list">
          <div
              class="style-item"
              v-for="(value, prop) in computedStyles"
              :key="prop"
          >
            <span class="prop">{{ prop }}:</span>
            <span class="value">{{ value }}</span>
          </div>
        </div>
      </div>

      <div class="empty-state" v-if="!selectedElement">
        <p>请选择页面元素进行检查</p>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onBeforeUnmount } from 'vue'

export default {
  name: 'ElementInspector',
  setup() {
    const inspectMode = ref('hover')
    const isInspecting = ref(false)
    const selectedElement = ref(null)
    const mouseX = ref(0)
    const mouseY = ref(0)

    const elementSize = computed(() => {
      if (!selectedElement.value) return ''
      const rect = selectedElement.value.getBoundingClientRect()
      return `${Math.round(rect.width)}px × ${Math.round(rect.height)}px`
    })

    const elementPosition = computed(() => {
      if (!selectedElement.value) return ''
      const rect = selectedElement.value.getBoundingClientRect()
      return `X: ${Math.round(rect.left)}, Y: ${Math.round(rect.top)}`
    })

    const computedStyles = computed(() => {
      if (!selectedElement.value) return {}

      const styles = window.getComputedStyle(selectedElement.value)
      const importantStyles = {}

      // 只显示部分重要的样式属性
      const styleProps = [
        'display', 'position', 'width', 'height',
        'margin', 'padding', 'border', 'background',
        'color', 'font-size', 'z-index', 'opacity'
      ]

      styleProps.forEach(prop => {
        importantStyles[prop] = styles.getPropertyValue(prop)
      })

      return importantStyles
    })

    const startInspection = () => {
      isInspecting.value = true
      document.addEventListener('mousemove', handleMouseMove)

      if (inspectMode.value === 'click') {
        document.addEventListener('click', handleElementClick, true)
      } else {
        document.addEventListener('mouseover', handleElementHover)
      }
    }

    const stopInspection = () => {
      isInspecting.value = false
      document.removeEventListener('mousemove', handleMouseMove)
      document.removeEventListener('click', handleElementClick, true)
      document.removeEventListener('mouseover', handleElementHover)
    }

    const handleMouseMove = (e) => {
      mouseX.value = e.clientX
      mouseY.value = e.clientY
    }

    const handleElementHover = (e) => {
      selectedElement.value = e.target
      e.stopPropagation()
    }

    const handleElementClick = (e) => {
      selectedElement.value = e.target
      e.stopPropagation()
      e.preventDefault()
    }

    onBeforeUnmount(() => {
      stopInspection()
    })

    return {
      inspectMode,
      isInspecting,
      selectedElement,
      elementSize,
      elementPosition,
      computedStyles,
      startInspection,
      stopInspection
    }
  }
}
</script>

<style scoped>
.element-inspector {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 15px;
}

.inspector-controls {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid var(--border-color);
}

.control-group {
  display: flex;
  align-items: center;
  gap: 15px;
}

.radio-group {
  display: flex;
  gap: 15px;
}

.radio-option {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 0.9rem;
}

.radio-option input[type="radio"] {
  position: absolute;
  opacity: 0;
}

.radio-custom {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-color);
  border-radius: 50%;
  margin-right: 8px;
  position: relative;
  transition: var(--transition);
}

.radio-option input:checked + .radio-custom {
  border-color: var(--primary-color);
}

.radio-option input:checked + .radio-custom::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary-color);
}

.inspector-content {
  flex: 1;
  overflow-y: auto;
}

.element-info, .element-styles {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
}

h3 {
  margin-bottom: 15px;
  color: var(--primary-color);
}

.info-row {
  display: flex;
  margin-bottom: 8px;
}

.label {
  width: 80px;
  color: var(--text-secondary);
}

.value {
  flex: 1;
}

.styles-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}

.style-item {
  background: rgba(0, 0, 0, 0.1);
  padding: 5px 10px;
  border-radius: 4px;
  font-family: 'Fira Code', monospace;
  font-size: 0.85rem;
}

.prop {
  color: var(--accent-color);
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-secondary);
}

.icon-start,
.icon-stop {
  display: inline-block;
  width: 16px;
  height: 16px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.icon-start {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%23ffffff'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z' /%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M21 12a9 9 0 11-18 0 9 9 0 0118 0z' /%3E%3C/svg%3E");
}

.icon-stop {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%23ffffff'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M21 12a9 9 0 11-18 0 9 9 0 0118 0z' /%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M9 10a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1v-4z' /%3E%3C/svg%3E");
}
</style>
